package com.hospital.data.simulation.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.pojo.TPatientInfo;
import com.hospital.data.simulation.exception.PatientInfoServiceException;
import com.hospital.data.simulation.exception.enums.PatientInfoServiceExpCode;
import com.hospital.data.simulation.mapper.TPatientInfoMapper;
import com.hospital.data.simulation.service.TPatientInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class TPatientInfoServiceImpl extends ServiceImpl<TPatientInfoMapper, TPatientInfo> implements TPatientInfoService{
    @Autowired
    private TPatientInfoMapper patientInfoMapper;
    @Override
    public Long addPatientInfo(TPatientInfo patientInfo) {
        if(patientInfoMapper.insert(patientInfo)==0){
            throw new PatientInfoServiceException(PatientInfoServiceExpCode.ADD_PATIENT_INFO_FAILED);
        }
        return patientInfo.getId();
    }

    @Override
    public void deletePatientInfo(Long id) {
        if (patientInfoMapper.deleteById(id)==0){
            throw new PatientInfoServiceException(PatientInfoServiceExpCode.DELETE_PATIENT_INFO_FAILED);
        }
    }

    @Override
    public void updatePatientInfo(TPatientInfo patientInfo) {
        LambdaUpdateWrapper<TPatientInfo> wrapper = new LambdaUpdateWrapper<>();
        constructWrapper(wrapper,patientInfo);
        if (patientInfoMapper.update(wrapper)==0){
            throw new PatientInfoServiceException(PatientInfoServiceExpCode.UPDATE_PATIENT_INFO_FAILED);
        }
    }
    private void constructWrapper(LambdaUpdateWrapper<TPatientInfo> wrapper, TPatientInfo patientInfo){
        wrapper.eq(TPatientInfo::getId,patientInfo.getId());
        if(!ObjectUtils.isEmpty(patientInfo.getIdentityCard())){
            wrapper.set(TPatientInfo::getIdentityCard,patientInfo.getIdentityCard());
        }
        if(!ObjectUtils.isEmpty(patientInfo.getAddress())){
            wrapper.set(TPatientInfo::getAddress,patientInfo.getAddress());
        }
        if(!ObjectUtils.isEmpty(patientInfo.getMedicalCard())){
            wrapper.set(TPatientInfo::getMedicalCard,patientInfo.getMedicalCard());
        }
    }
}
